/***********************************************************************************************************
 *** @Author         : jiabao
 *** @Description    :
 *** @Date           : 2023-12-02 15:36:59
 *** @FilePath       : /demo_cc/socket/multicast/main.cpp
 ***********************************************************************************************************/
#include <iostream>
#include <thread>
#include "multicast.hpp"
using namespace std;

Multicast *pmulticast = NULL;
void thread_socket_revc(string ip, int port)
{
    Multicast multicast(ip, port);
    pmulticast = &multicast;
    struct sockaddr_in addrFrom;
    int revcLen = 0;
    string revcStr;
    revcStr.reserve(1024);
    while (1)
    {
        revcLen = multicast.multicastRead(revcStr, &addrFrom);
        cout << "receiveIp:" << inet_ntoa(addrFrom.sin_addr) << endl;
        cout << "receivePort:" << addrFrom.sin_port << endl;
        cout << "receiveLen:" << revcLen << endl;
        cout << "recv:" << revcStr << endl;
    }
}

void thread_socket_send()
{
    string sendStr = "hello world\n";
    while (1)
    {
        if (pmulticast != NULL)
        {
            printf("send\n");
            pmulticast->multicastSend(sendStr);
        }
        else
        {
            cout << "???" << endl;
        }
        sleep(5);
    }
}

int main()
{
    thread t1(thread_socket_revc, "234.5.6.7", 8888);
    // thread t2(thread_socket_send);
    t1.join();
    // t2.join();
    while (1)
    {
        cout << "jiabao!" << endl;
        sleep(1);
    }
}
